<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: checkout_payment_address.php 160 2013-09-22 07:53:27Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 160 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:53:27 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include ('includes/application_top.php');

$smarty = new Smarty;

require (DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');
require_once (DIR_FS_INC.'inc.count_customer_address_book_entries.php');
require_once (DIR_FS_INC.'inc.address_label.php');

if (is_array($_SESSION['nvpReqArray']))
	$link_checkout_payment = FILENAME_PAYPAL_CHECKOUT;
else
	$link_checkout_payment = FILENAME_CHECKOUT_PAYMENT;

if (!isset ($_SESSION['customer_id']))
	redirect(href_link(FILENAME_LOGIN, '', 'SSL'));

if ($_SESSION['cart']->count_contents() < 1)
	redirect(href_link(FILENAME_SHOPPING_CART));

$addresses_count = count_customer_address_book_entries();

if($addresses_count < MAX_ADDRESS_BOOK_ENTRIES)
	require (DIR_WS_MODULES.'checkout_new_address.php');

$process = false;
if (isset($_POST['action']) && ($_POST['action'] == 'process') && $field->error != 'true') {
	if (!empty($field->post_vars['firstname']) && !empty($field->post_vars['lastname']) && !empty($field->post_vars['street_address'])) {
		$process = true;
		
		$sql_data_array = array('customers_id' => $_SESSION['customer_id'], 
								'entry_firstname' => $field->post_vars['firstname'], 
								'entry_lastname' => $field->post_vars['lastname'], 
								'entry_street_address' => $field->post_vars['street_address'], 
								'entry_postcode' => $field->post_vars['postcode'], 
								'entry_city' =>  $field->post_vars['city'], 
								'entry_country_id' =>  $field->post_vars['country']);

		if (!empty($field->post_vars['gender']))
			$sql_data_array['entry_gender'] = $field->post_vars['gender'];
		if (!empty($field->post_vars['company']))
			$sql_data_array['entry_company'] = $field->post_vars['company'];
		if (!empty($suburb))
			$sql_data_array['entry_suburb'] = $field->post_vars['suburb'];
		if (isset($field->post_vars['zone_id'])) {
			if ($field->post_vars['zone_id'] > 0) {
				$sql_data_array['entry_zone_id'] = $field->post_vars['zone_id'];
				$sql_data_array['entry_state'] = '';
			} else {
				$sql_data_array['entry_zone_id'] = '0';
				$sql_data_array['entry_state'] = $field->post_vars['state'];
			}
		}

		$db->db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);

		$_SESSION['billto'] = $db->db_insert_id();

		if (isset($_SESSION['payment']))
			unset($_SESSION['payment']);
		
		$db->db_query("UPDATE 
							".TABLE_CUSTOMERS_INFO." 
						SET 
							customers_info_date_account_last_modified = NOW() 
						WHERE 
							customers_info_id = '".(int)$_SESSION['customer_id']."'");
		
		redirect(href_link($link_checkout_payment, '', 'SSL'));

	} elseif (isset ($_POST['address'])) {
		$reset_payment = false;
		if (isset ($_SESSION['billto']))
			if ($billto != $_POST['address'])
				if (isset ($_SESSION['payment']))
					$reset_payment = true;

		$_SESSION['billto'] = $_POST['address'];

		$check_address = $db->db_query("SELECT COUNT(*) AS total from ".TABLE_ADDRESS_BOOK." WHERE customers_id = '".$_SESSION['customer_id']."' and address_book_id = '".$_SESSION['billto']."'");

		if ($check_address->fields['total'] == '1') {
			if ($reset_payment == true)
				unset ($_SESSION['payment']);
			redirect(href_link($link_checkout_payment, '', 'SSL'));
		} else
			unset ($_SESSION['billto']);
		
		// no addresses to SELECT from - customer decided to keep the current assigned address
	} else {
		$_SESSION['billto'] = $_SESSION['customer_default_address_id'];
		redirect(href_link($link_checkout_payment, '', 'SSL'));
	}
}

if (!isset ($_SESSION['billto']))
	$_SESSION['billto'] = $_SESSION['customer_default_address_id'];

$breadcrumb->add(NAVBAR_TITLE_1_PAYMENT_ADDRESS, href_link($link_checkout_payment, '', 'SSL'));
$breadcrumb->add(NAVBAR_TITLE_2_PAYMENT_ADDRESS, href_link(FILENAME_CHECKOUT_PAYMENT_ADDRESS, '', 'SSL'));

require (DIR_WS_INCLUDES.'header.php');

if ($message_stack->size('checkout_address') > 0)
	$smarty->assign('error', $message_stack->output('checkout_address'));

if ($process == false) {
	$smarty->assign('ADDRESS_LABEL', address_label($_SESSION['customer_id'], $_SESSION['billto'], true, ' ', '<br />'));

	if ($addresses_count > 1) {

		$address_content = '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
		$radio_buttons = 0;

		$addresses = $db->db_query("SELECT 
										address_book_id, 
										entry_firstname AS firstname, 
										entry_lastname AS lastname, 
										entry_company AS company, 
										entry_street_address AS street_address, 
										entry_suburb AS suburb, 
										entry_city AS city, 
										entry_postcode AS postcode, 
										entry_state AS state, 
										entry_zone_id AS zone_id, 
										entry_country_id AS country_id 
									FROM 
										".TABLE_ADDRESS_BOOK." 
									WHERE 
										customers_id = '".$_SESSION['customer_id']."'");
		while (!$addresses->EOF) {
			$format_id = get_address_format_id($addresses->fields['country_id']);
			$address_content .= ' <tr>
			                <td>&nbsp;</td>
			                <td colspan="2"><table border="0" width="100%" cellspacing="0" cellpadding="2">
			                ';
			if ($addresses->fields['address_book_id'] == $_SESSION['billto']) {
				$address_content .= '                  <tr id="defaultSelected" class="moduleRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, '.$radio_buttons.')">'."\n";
			} else {
				$address_content .= '                  <tr class="moduleRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, '.$radio_buttons.')">'."\n";
			}
			$address_content .= '
			                    <td>&nbsp;</td>
			                    <td class="main" colspan="2"><strong>'.$addresses->fields['firstname'].' '.$addresses->fields['lastname'].'</strong></td>
			                    <td class="main" align="right">'.draw_radio_field('address', $addresses->fields['address_book_id'], ($addresses->fields['address_book_id'] == $_SESSION['billto'])).'</td>
			                    <td>&nbsp;</td>
			                  </tr>
			                  <tr>
			                    <td>&nbsp;</td>
			                    <td colspan="3"><table border="0" cellspacing="0" cellpadding="2">
			                      <tr>
			                        <td>&nbsp;</td>
			                        <td class="main">'.address_format($format_id, $addresses->fields, true, ' ', ', ').'</td>
			                        <td>&nbsp;</td>
			                      </tr>
			                    </table></td>
			                    <td>&nbsp;</td>
			                  </tr>
			                </table></td>
			                <td>&nbsp;</td>
			              </tr>';

			$radio_buttons ++;
			$addresses->MoveNext();
		}
		$address_content .= '</table>';
		$smarty->assign('BLOCK_ADDRESS', $address_content);
	}
}

$smarty->assign('FORM_ACTION', draw_form('checkout_address', href_link(FILENAME_CHECKOUT_PAYMENT_ADDRESS, '', 'SSL')).draw_hidden_field('action', 'process'));
	
$smarty->assign('BUTTON_CONTINUE', image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE));

if ($process == true) {
	$smarty->assign('BUTTON_BACK', '<a href="'.href_link(FILENAME_CHECKOUT_PAYMENT_ADDRESS, '', 'SSL').'">'.image_button('button_back.gif', IMAGE_BUTTON_BACK).'</a>');
}
$smarty->assign('FORM_END', '</form>');
$smarty->assign('language', $_SESSION['language']);

$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/checkout_payment_address.html');

$smarty->assign('language', $_SESSION['language']);
$smarty->assign('main_content', $main_content);
$smarty->caching = false;
if (!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');
?>